<?php

namespace app\ruinuo\controller\design;

use app\common\model\RuinuoSetting;
use app\common\service\Logic as LogicService;
use app\ruinuo\design\FormLayout as CurdForm;
use think\facade\Cache;
use think\facade\Request;
use think\facade\View;
use app\common\Service\Validate as ValidateService;
use app\common\middleware\Auth;
class Setting
{
    protected $middleware = [Auth::class => []];
    public function Index()
    {
        $id = input("id") ?: 0;
        $RuinuoSetting = new RuinuoSetting();
        $find = $RuinuoSetting->where("id='" . $id . "'")->field("layout,name,label_width,form_label")->findOrEmpty()->toArray();
        if (empty($find)) {

            return BackCode([], '203', '没有这条数据');
        }
        $layout = !empty($find['layout']) ? json_decode($find['layout'], true) : [];
        $CurdForm = new CurdForm();
        $html = $CurdForm->GetForm($layout);
        $js = $CurdForm->GetJs($layout);
        $fields = $CurdForm->GetFormField($layout);
        $onclick = [];
        foreach ($fields as $k => $v) {
            $event_val = !empty($v['event_val']) ? $v['event_val'] : [];
            $gongshi_txt = !empty($v['gongshi_txt']) ? $v['gongshi_txt'] : '';
            foreach ($event_val as $k2 => $v2) {
                $v2['field'] = $v['field'];
                $v2['tag'] = $v['tag'];
                $onclick[] = $v2;
            }
            if ($gongshi_txt) {
                $onclick[] = [
                    'field' => $v['field'],
                    'tag' => $v['tag'],
                    'gongshi_txt' => $gongshi_txt,
                ];
            }
        }
        $onclick = json_encode($onclick, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        View::assign('onclick', $onclick);
        View::assign('html', $html);
        View::assign('js', $js);
        View::assign('name', $find['name']);
        View::assign('label_width', $find['label_width']);
        View::assign('form_label', $find['form_label']);
        return ViewTpl('ruinuo/design/setting');
    }

    public function Edit()
    {
        if (Request::isAjax() || Request::isPost()) {
            $id = input("id") ?: 0;
            $RuinuoSetting = new RuinuoSetting();
            $find = $RuinuoSetting->where("id='" . $id . "'")->field("layout,num")->findOrEmpty()->toArray();
            if (empty($find)) {
                return BackCode([], '203', '没有这条数据');
            }
            $CurdForm = new CurdForm();
            $layout = !empty($find['layout']) ? json_decode($find['layout'], true) : [];
            $fields = $CurdForm->GetFormField($layout);
            $ItemData = [];
            $values = input();
            foreach ($fields as $k => $v) {
                $field = $v['field'];
                $value = isset($v['value']) ? $v['value'] : '';
                $ItemData[$field] = isset($values[$field]) ? $values[$field] : $value;
            }
            $ValidateService = new ValidateService();
            $LogicService = new LogicService();
            $Validate = $LogicService->Validate(['layout' => $fields]);
            foreach ($Validate as $k => $v) {
                if ($v['rule'] == 'only') {
                    unset($Validate[$k]);
                }
            }
            $Validate = array_values($Validate);
            $check = $ValidateService->Rule($Validate, ['a' => $ItemData], [], ['sub_database' => []], $id);
            if ($check['code'] == '203') {
                return $check;
            }
            $upData['values'] = json_encode($ItemData, JSON_UNESCAPED_UNICODE);
            $upData['update_id'] = GetToken('id');
            $upData['update_time'] = date('Y-m-d H:i:s');
            $RuinuoSetting->where("id='" . $id . "'")->update($upData);
            $back['id'] = $id;
            $CacheId='ruinuo_setting_code_'.$find['num'];
            Cache::set($CacheId, [], 600);
            return ['code' => 200, 'msg' => '修改成功', 'data' => $back];
        }
    }

    public function Detail()
    {
        if (Request::isAjax() || Request::isPost()) {
            $id = input("id") ?: 0;
            $RuinuoSetting = new RuinuoSetting();
            $find = $RuinuoSetting->where("id='" . $id . "'")->field("layout,values")->findOrEmpty()->toArray();
            if (empty($find)) {
                return BackCode([], '203', '没有这条数据');
            }
            $CurdForm = new CurdForm();
            $layout = !empty($find['layout']) ? json_decode($find['layout'], true) : [];
            $values = !empty($find['values']) ? json_decode($find['values'], true) : [];
            $fields = $CurdForm->GetFormField($layout);
            $LogicService = new LogicService();
            $data['source'] = $LogicService->GetSource(['layout' => $fields]);
            $data['detail'] = [];
            foreach ($fields as $k => $v) {
                $field = $v['field'];
                $value = isset($v['value']) ? $v['value'] : '';
                $data['detail'][$field] = isset($values[$field]) ? $values[$field] : $value;
            }
            $data['detail']['id'] = $id;
            return BackCode($data);
        }
    }


}